package zjut.raymond.witdemo.viewmodel;

import android.os.Handler;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.ArrayList;
import java.util.List;

import zjut.raymond.witdemo.pojo.ReferencePoint;

public class CollectorViewModel extends ViewModel {
    /**
    * referencePointsLive
     */
    private MutableLiveData<List<ReferencePoint>> referencePointsLive;
    private List<ReferencePoint> referencePoints = new ArrayList<>();

    public MutableLiveData<List<ReferencePoint>> getReferencePointsLive() {
        if (referencePointsLive == null) {
            referencePointsLive = new MutableLiveData<>();
            loadReferencePoint();
        }
        return referencePointsLive;
    }
    public void loadReferencePoint() {
        Handler handler = new Handler();
        handler.postDelayed(() -> referencePointsLive.postValue(referencePoints), 1000);
    }

    public void addReferencePoint(ReferencePoint referencePoint) {
        if (!referencePoints.contains(referencePoint)) {
            referencePoints.add(referencePoint);
            referencePointsLive.postValue(referencePoints);
        } else {
            for (ReferencePoint r : referencePoints) {
                if (r.getId() == referencePoint.getId()) {
                    r.setUpdateTime(referencePoint.getUpdateTime());
                    break;
                }
            }
        }
    }

    public void clearReferencePoint() {
        referencePoints.clear();
        referencePointsLive.postValue(referencePoints);
    }

    public boolean isReferencePointExist(int id) {
        for (ReferencePoint r : referencePoints) {
            if (id == r.getId()) {
                return true;
            }
        }
        return false;
    }
    /**
     * 扫描次数
     */
    private MutableLiveData<Integer> scanCount;

    public MutableLiveData<Integer> getScanCount() {
        if (scanCount == null) {
            scanCount = new MutableLiveData<>();
            scanCount.setValue(0);
        }
        return scanCount;
    }

    public void addScanCount() {
        scanCount.setValue(scanCount.getValue() + 1);
    }

    public void clearScanCount() {
        scanCount.setValue(0);
    }
}
